package com.herui.sdyu_lib.aliyun.oss;

/**
 * Created by amanda.shan on 2016/11/14.
 */
public class OssImageProcess {

    private StringBuilder url;

    public static OssImageProcess buildUrlNotAction(String objectKey) {

        OssImageProcess ossImageProcess = new OssImageProcess();
        ossImageProcess.url = new StringBuilder();
        ossImageProcess.url.append("http://");
        ossImageProcess.url.append(AliOssConfig.bucketEendpoint);
        ossImageProcess.url.append("/");
        ossImageProcess.url.append(objectKey);

        return ossImageProcess;
    }

    public static OssImageProcess buildUrl(String objectKey) {

        OssImageProcess ossImageProcess = new OssImageProcess();
        ossImageProcess.url = new StringBuilder();
        ossImageProcess.url.append("http://");
        ossImageProcess.url.append(AliOssConfig.bucketEendpoint);
        ossImageProcess.url.append("/");
        ossImageProcess.url.append(objectKey);
        ossImageProcess.url.append("?x-oss-process=image");

        return ossImageProcess;
    }

    public static OssImageProcess buildAction() {
        OssImageProcess ossImageProcess = new OssImageProcess();
        ossImageProcess.url = new StringBuilder();

        return ossImageProcess;
    }

    /**
     * 等比缩放，限制在设定在指定w与h的矩形内的最大图片
     *
     * @param w 要缩放的宽度
     * @param h 要缩放的高度
     * @return
     */
    public OssImageProcess setImageResize(Integer w, Integer h) {

        if (url == null)
            url = new StringBuilder();
        url.append("/");
        url.append("resize,m_fill,");
        if (w != null)
            url.append(String.format("w_%d,", w.intValue()));
        if (h != null)
            url.append(String.format("h_%d,", h.intValue()));

        url.deleteCharAt(url.length() - 1);

        return this;
    }

    public OssImageProcess cropImageResize(Integer w, Integer h){
        if (url == null)
            url = new StringBuilder();
        url.append("/");
        url.append("crop,x_0,y_0,");

        if (w != null)
            url.append(String.format("w_%d,", w.intValue()));
        if (h != null)
            url.append(String.format("h_%d,", h.intValue()));

        url.append("g_center");

        return this;
    }

    /**
     * 从图片取出的圆形区域的半径
     *
     * @param r 半径 r 不能超过原图的最小边的一半。如果超过，则圆的大小仍然是原圆的最大内切圆。
     * @return
     */
    public OssImageProcess setImageCircle(int r) {

        if (url == null)
            url = new StringBuilder();

        url.append("/");
        url.append(String.format("circle,r_%d/format,png", r));

        return this;
    }

    /**
     * 设置图片处理的action
     * @param action
     * @return
     */
    public OssImageProcess setAction(String action) {
        if (url == null)
            url = new StringBuilder();
        url.append(action);
        return this;
    }

    public String getUrl() {
        return url.toString();
    }
}
